package com.eazyshop.goods.service.impl;


import com.alibaba.fastjson.JSON;
import com.eazyshop.common.utils.IdWorker;
import com.eazyshop.goods.dao.*;
import com.eazyshop.goods.pojo.*;
import com.eazyshop.goods.service.SkuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SkuServiceImpl implements SkuService {

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BrandMapper brandMapper;


    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Autowired
    private SpuMapper spuMapper;
    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<Sku> findAll() {
        return skuMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Sku findById(String id){
        return  skuMapper.selectByPrimaryKey(id);
    }


    /**
     * 增加
     * @param goods
     */
    @Override
    @Transactional
    public void add(Goods goods){
        Spu spu = goods.getSpu();
        // 设置id
        spu.setId(String.valueOf(idWorker.nextId()));
        // 设置审核状态
        spu.setStatus("0");
        // 设置删除状态
        spu.setIsDelete("0");
        // 设置上架状态
        spu.setIsMarketable("0");
        // 添加spu到goods
        goods.setSpu(spu);
        // 保存spu
        spuMapper.insert(spu);
        // 添加sku
        this.saveSkuList(goods);
    }

    private void saveSkuList(Goods goods) {
        Spu spu = goods.getSpu();
        List<Sku> skuList = goods.getSkuList();
        Integer category3Id = spu.getCategory3Id();
        // 添加品牌与分类的关系
        Category category = categoryMapper.selectByPrimaryKey(category3Id);
        Integer brandId = spu.getBrandId();
        CategoryBrand categoryBrand = new CategoryBrand();

        categoryBrand.setBrandId(brandId);
        categoryBrand.setCategoryId(category3Id);
        int i = categoryBrandMapper.selectCount(categoryBrand);

        if(i==0){
            categoryBrandMapper.insertSelective(categoryBrand);
        }
        // 判断是否为空
        if(skuList!=null){
            for (Sku sku : skuList) {
                // 设置id
                sku.setId(String.valueOf(idWorker.nextId()));
                // 设置对应的分类名
                Integer categoryId = spu.getCategory3Id();
                String categoryName = categoryMapper.selectByPrimaryKey(categoryId).getName();
                sku.setCategoryName(categoryName);
                // 设置sku规格
                if(sku.getSpec()==null||"".equals(sku.getSpec())){
                    sku.setSpec("{}");
                }
                // 把json转为map集合
                Map<String, String> map = JSON.parseObject(sku.getSpec(), Map.class);
                String name = sku.getName();
                for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
                    // 以空格隔开
                    name+=" "+stringStringEntry.getValue();
                }
                // 设置name
                sku.setName(name);
                // 设置spu的id
                sku.setSpuId(spu.getId());
                // 创建时间
                sku.setCreateTime(new Date());
                // 修改时间
                sku.setUpdateTime(new Date());
                // 商品分类id
                sku.setCategoryId(category.getId());
                // 商品分类名字
                sku.setCategoryName(category.getName());
                // 插入sku数据库
                skuMapper.insertSelective(sku);
            }
        }
    }


    /**
     * 修改
     * @param sku
     */
    @Override
    public void update(Sku sku){
        skuMapper.updateByPrimaryKey(sku);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        skuMapper.deleteByPrimaryKey(id);
    }


    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @Override
    public List<Sku> findList(Map<String, Object> searchMap){

        Example example = createExample(searchMap);
        return skuMapper.selectByExample(example);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Sku> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Sku>)skuMapper.selectAll();
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Sku> findPage(Map<String,Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Sku>)skuMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Sku.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 商品id
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andEqualTo("id", searchMap.get("id"));
           	}
            // 商品条码
            if(searchMap.get("sn")!=null && !"".equals(searchMap.get("sn"))){
                criteria.andEqualTo("sn",searchMap.get("sn"));
           	}
            // SKU名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 商品图片
            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                criteria.andLike("image","%"+searchMap.get("image")+"%");
           	}
            // 商品图片列表
            if(searchMap.get("images")!=null && !"".equals(searchMap.get("images"))){
                criteria.andLike("images","%"+searchMap.get("images")+"%");
           	}
            // SPUID
            if(searchMap.get("spuId")!=null && !"".equals(searchMap.get("spuId"))){
                criteria.andEqualTo("spuId",searchMap.get("spuId"));
           	}
            // 类目名称
            if(searchMap.get("categoryName")!=null && !"".equals(searchMap.get("categoryName"))){
                criteria.andLike("categoryName","%"+searchMap.get("categoryName")+"%");
           	}
            // 品牌名称
            if(searchMap.get("brandName")!=null && !"".equals(searchMap.get("brandName"))){
                criteria.andLike("brandName","%"+searchMap.get("brandName")+"%");
           	}
            // 规格
            if(searchMap.get("spec")!=null && !"".equals(searchMap.get("spec"))){
                criteria.andLike("spec","%"+searchMap.get("spec")+"%");
           	}
            // 商品状态 1-正常，2-下架，3-删除
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status", searchMap.get("status"));
           	}

            // 价格（分）
            if(searchMap.get("price")!=null ){
                criteria.andEqualTo("price",searchMap.get("price"));
            }
            // 库存数量
            if(searchMap.get("num")!=null ){
                criteria.andEqualTo("num",searchMap.get("num"));
            }
            // 库存预警数量
            if(searchMap.get("alertNum")!=null ){
                criteria.andEqualTo("alertNum",searchMap.get("alertNum"));
            }
            // 重量（克）
            if(searchMap.get("weight")!=null ){
                criteria.andEqualTo("weight",searchMap.get("weight"));
            }
            // 类目ID
            if(searchMap.get("categoryId")!=null ){
                criteria.andEqualTo("categoryId",searchMap.get("categoryId"));
            }
            // 销量
            if(searchMap.get("saleNum")!=null ){
                criteria.andEqualTo("saleNum",searchMap.get("saleNum"));
            }
            // 评论数
            if(searchMap.get("commentNum")!=null ){
                criteria.andEqualTo("commentNum",searchMap.get("commentNum"));
            }

        }
        return example;
    }

}
